package com.ymatou.iapi.productimporter.testcase;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productimporter.parameter.CreateBean;
import com.ymatou.iapi.productimporter.parameter.GetTaskDetailBean;
import com.ymatou.iapi.productimporter.parameter.ProducFailInfo;
import com.ymatou.iapi.productimporter.parameter.TaobaoZhuShouExportBean;
import com.ymatou.iapi.productimporter.parameter.TaskDetail;
import com.ymatou.iapi.productimporter.parameter.TaskInfo;
import com.ymatou.iapi.productimporter.service.GetTaskDetailCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductImporterCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.ProductImportDetail;
import com.ymttest.database.model.ProductImportTask;
import com.ymttest.database.sqlwapper.ProductImporterWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
*批量查询任务
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTaskDetail{
	private static GetTaskDetailBean gettaskdetailBean;
	private static GetTaskDetailCall gettaskdetailCall;
	private static int vipRootUserId = AgentUsers.vipRootUserId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询任务");
	}

	@Before
	public void caseUp() {
		gettaskdetailBean=new GetTaskDetailBean();
		gettaskdetailCall=new GetTaskDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettaskdetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(GetTaskDetailBean gettaskdetailBean){
		String data = gettaskdetailCall.getJsonObject().get("data").toString();
		TaskDetail taskDetail = (TaskDetail)FormatUtil.GSONToObject(data, TaskDetail.class);
		TaskInfo taskInfo = taskDetail.getTaskInfo();
		List<ProducFailInfo> productFailList = taskDetail.getProductFailList();

		ProductImporterWapper productImporterWapper = new ProductImporterWapper();
		List<ProductImportDetail> successProduct = productImporterWapper.selectProductImportDetailByTaskIdWithIsSuccess(gettaskdetailBean.getTaskId(),true);
		List<ProductImportDetail> failProduct = productImporterWapper.selectProductImportDetailByTaskIdWithIsSuccess(gettaskdetailBean.getTaskId(),false);
		List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(gettaskdetailBean.getTaskId());
		ProductImportTask productImportTask = productImporterWapper.selectProductImportTaskByTaskId(gettaskdetailBean.getTaskId());

		Logger.verifyEquals(taskInfo.getTaskId(),gettaskdetailBean.getTaskId(),"验证sellerid");
		Logger.verifyEquals(taskInfo.getTaskStatus(),productImportTask.getTaskStatus(),"验证taskStatus");
		Logger.verifyEquals(taskInfo.getFileName(),productImportTask.getLocalFilename(),"验证fileName");
		Logger.verifyEquals(taskInfo.getRemark(),productImportTask.getTaskStatusMsg(),"验证Remark");
		if (taskInfo.getTaskStatus()==1){
			Logger.verifyEquals(taskInfo.getTotalNum(),0,"验证totalNum");
			Logger.verifyEquals(taskInfo.getSuccessNum(),0,"验证successNum");
		}else{
			Logger.verifyEquals(taskInfo.getTotalNum(),productImportDetails.size(),"验证totalNum");
			Logger.verifyEquals(taskInfo.getSuccessNum(),successProduct.size(),"验证successNum");
		}
		if (taskInfo.getFailNum()>0){
			Logger.verifyEquals(taskInfo.getFailNum(),failProduct.size(),"验证failNum");
		}

		if (productFailList!=null){
			productFailList.forEach(p->{
				failProduct.forEach(x->{
					if (p.getProductTitle().equals(x.getSourceProductTitle())){
						Logger.verifyEquals(p.getFailReason(),x.getFailReasonDes(),"验证failReason");
					}
				});
			});
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskDetail_01() {
		Logger.start(true,"任务详情，执行完成");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品"+TestDataManager.getRandomNum(6), "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(2000);
			gettaskdetailBean.setSellerId(vipRootUserId);
			gettaskdetailBean.setTaskId(taskId);
			gettaskdetailBean.setAppId(TestDataManager.getRandomNum(6));
			gettaskdetailCall.setData(gettaskdetailBean);
			gettaskdetailCall.callService();
			verifyDB(gettaskdetailBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskDetail_02() {
		Logger.start(true,"任务详情,执行完成，含有失败商品");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			List<TaobaoZhuShouExportBean> datas = new ArrayList<>();
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
			TaobaoZhuShouExportBean tb2 = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
			TaobaoZhuShouExportBean tb1 = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,null);
			datas.add(tb);
			datas.add(tb1);
			datas.add(tb2);

			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl= TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(5000);
			gettaskdetailBean.setSellerId(vipRootUserId);
			gettaskdetailBean.setTaskId(taskId);
			gettaskdetailBean.setAppId(TestDataManager.getRandomNum(6));
			gettaskdetailCall.setData(gettaskdetailBean);
			gettaskdetailCall.callService();
			verifyDB(gettaskdetailBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskDetail_03() {
		Logger.start(true,"任务详情,正在导入");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			List<TaobaoZhuShouExportBean> datas = new ArrayList<>();
			for (int i=0;i<10;i++){
				TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
				datas.add(tb);
			}
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl= TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			gettaskdetailBean.setSellerId(vipRootUserId);
			gettaskdetailBean.setTaskId(taskId);
			gettaskdetailBean.setAppId(TestDataManager.getRandomNum(6));
			gettaskdetailCall.setData(gettaskdetailBean);
			gettaskdetailCall.callService();
			verifyDB(gettaskdetailBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskDetail_04() {
		Logger.start(true,"任务详情,导入失败");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl+"c");
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(2000);
			gettaskdetailBean.setSellerId(vipRootUserId);
			gettaskdetailBean.setTaskId(taskId);
			gettaskdetailBean.setAppId(TestDataManager.getRandomNum(6));
			gettaskdetailCall.setData(gettaskdetailBean);
			gettaskdetailCall.callService();
			verifyDB(gettaskdetailBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}